package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.QueryPageDTO;
import com.itheima.reggie.common.Result;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Yan
 * @date 2022/2/25
 */
@RestController
@RequestMapping("category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


    /**
     * 新增
     * @param category
     * @return
     */
    @PostMapping
    public Result<String> save(@RequestBody Category category){
        return categoryService.saveCategory(category);
    }

    /**
     * 分页查询
     * @param queryPageDTO
     * @return
     */
    @GetMapping("page")
    public Result<Page<Category>> page(QueryPageDTO queryPageDTO){
        return categoryService.findByPage(queryPageDTO);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping
    public Result<String> deleteCategory(Long id){
        return categoryService.deleteCategory(id);
    }

    @PutMapping
    public Result<String> updateCategory(@RequestBody Category category){
        return categoryService.updateCategory(category);
    }

    /**
     * 展示分类
     * @param type
     * @return
     */
    @GetMapping("list")
    public Result<List<Category>> list(@RequestParam(required = false) Integer type){
        return categoryService.listCategory(type);
    }

}
